﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ThisIsFirstTest.Models.MealModel;
using MealBooking.Core;
using ThisIsFirstTest.Models.OrderModel;
using MealBooking.Core.Models.Meals;
using MealBooking.Core.Models.Accounts;
using MealBooking.Core.Models.Orders;

namespace ThisIsFirstTest.Controllers.OrderController
{
    public class OrderController : Controller
    {
        public ActionResult Index(string headerMessage = null)
        {
            if (headerMessage != null)
            {
                ViewBag.HeaderTitle = headerMessage;
            }
            else
            {
                ViewBag.HeaderTitle = "My Order";
            }
            if (User.Identity.IsAuthenticated)
            {
                var account = new Account { Id = User.Identity.Name };
                account = CoreService.AccountService.AccountDetail(account);
                var order = CoreService.OrderService.UserTodayOrder(account.Id);
                return View(order);
            }
            return View();
        }

        #region Order
        public ActionResult OrderDetail(string id, string headerMessage = null)
        {
            var order = CoreService.OrderService.OrderDetail(id);
            if (Request.IsAuthenticated)
            {
                if (order != null && User.Identity.Name.Equals(order.Account.Id) && order.CreatedTime.Date == DateTime.Now.Date)
                {
                    return RedirectToAction("TodayOrder");
                }
            }
            ViewBag.HeaderTitle = headerMessage + "Order Detail";
            return View(order);
        }
        #endregion

        #region User Order
        [Authorize]
        public ActionResult TodayOrder(string headerMessage = null)
        {
            if (headerMessage != null)
            {
                ViewBag.HeaderTitle = headerMessage;
            }
            else
            {
                ViewBag.HeaderTitle = "Order Detail";
            }
            Order order = CoreService.OrderService.UserTodayOrder(User.Identity.Name);
            if (order == null)
            {
                return RedirectToAction("Index");
            }
            return View(order);
        }
        public ActionResult HistoryOrder()
        {
            var historyOrders = CoreService.OrderService.Orders(User.Identity.Name);
            return View(historyOrders);
        }
        [Authorize]
        public ActionResult EditTodayOrder()
        {
            Order order = CoreService.OrderService.UserTodayOrder(User.Identity.Name);
            if (order == null)
            {
                return RedirectToAction("Index");
            }
            if (order.IsBooked)
            {
                return RedirectToAction("TodayOrder", new { headerMessage = "Already Booked. " });
            }
            return View(order);
        }
        [HttpPost]
        [Authorize]
        public ActionResult EditTodayOrder(EditTodayOrderModel newOrder)
        {
            var order = CoreService.OrderService.UserTodayOrder(User.Identity.Name);
            #region validate
            if (order == null)
            {
                return RedirectToAction("Index");
            }
            if (order.IsBooked)
            {
                return RedirectToAction("TodayOrder", new { headerMessage = "Already Booked. " });
            }
            #endregion
            if (newOrder.MealsWithSum == null || newOrder.MealsWithSum.Count == 0)
            {
                order.MealsWithSum = null;
            }
            else
            {
                var mealsWithSum = newOrder.MealsWithSum.Select(m =>
                new MealWithSum { Meal = new Meal { Id = m.Id }, MealSum = m.MealSum });
                order.MealsWithSum = mealsWithSum.ToList();
            }
            CoreService.OrderService.UpdateUserTodayOrder(order);
            return RedirectToAction("TodayOrder", new { headerMessage = "Update Success. " });
        }
        [HttpPost]
        [Authorize]
        public ActionResult OrderTodayOrder()
        {
            #region validate
            //get logon user:
            var accouont = CoreService.AccountService.AccountDetail(new Account { Id = User.Identity.Name });
            if (!accouont.IsActived)
            {
                return RedirectToAction("Index", "Account", new { headerMessage = "Account is not Actived." });
            }
            var customer = CoreService.AccountService.CustomerDetail(User.Identity.Name);
            if (customer.Credit + customer.Balance < 0)
            {
                return RedirectToAction("Index", "Account", new { headerMessage = "Not Enough Balance." });
            }
            //manager booked today order:
            var manager = CoreService.AccountService.AllManagerInformations().FirstOrDefault();
            var managerOrder = CoreService.OrderService.ManagerTodayOrder(manager.Id);
            if (managerOrder != null && managerOrder.IsBooked)
            {
                return RedirectToAction("CustomersTodayOrders", "Order", new { headerMessage = "Already Booked. " });
            }
            var order = CoreService.OrderService.UserTodayOrder(User.Identity.Name);
            if (order == null)
            {
                return RedirectToAction("Index");
            }
            if (order.IsBooked)
            {
                return RedirectToAction("TodayOrder", new { headerMessage = "Already Booked. " });
            }
            #endregion
            CoreService.OrderService.OrderUserTodayOrder(customer.Id);
            return RedirectToAction("TodayOrder", new { headerMessage = "Order Success. " });
        }
        [HttpPost]
        [Authorize]
        public ActionResult DeleteTodayOrder()
        {
            #region validate
            //get logon user:
            var order = CoreService.OrderService.UserTodayOrder(User.Identity.Name);
            if (order == null)
            {
                return RedirectToAction("Index");
            }
            if (order.IsBooked)
            {
                return RedirectToAction("TodayOrder", new { headerMessage = "Already Booked. Can not Delete." });
            }
            #endregion
            CoreService.OrderService.DeleteUserTodayOrder(User.Identity.Name);
            return RedirectToAction("Index", new { headerMessage = "Delete The Order Success. " });
        }
        #endregion

        #region All Customers' Today Orders
        public ActionResult CustomersTodayOrders(string headerMessage = null)
        {
            ViewBag.HeaderTitle = headerMessage + "Order Details";
            var orders = CoreService.OrderService.TodayOrders();
            return View(orders);
        }
        #endregion

    }
}
